from typing import List


class Solution:
    def maximumLength(self, nums: List[int], k: int) -> int:
        if len(nums) < 2:
            return len(nums)
        # nums.sort()
        # print(nums)
        dp = []
        max_len = 0
        for i in range(0, len(nums)):
            if i + 1 >= len(nums):
                # 最后一个
                if len(dp) > k:
                    if len(dp) == 0:
                        max_len = max(max_len, len(nums))
                    else:
                        max_len = max(max_len, len(nums) - dp[0] - 1)
                    print(dp)
                    print("t1:", max_len)
                else:
                    print(dp)
                    max_len = max(max_len, len(nums))
                    print("t2:", max_len)
            else:
                if nums[i] != nums[i + 1]:
                    if len(dp) > k:
                        max_len = max(max_len, i - dp[0])
                        dp.pop(0)
                        print("t3:", max_len)
                    else:
                        print(dp)
                        max_len = max(max_len, i + 1)
                        print("t4:", max_len)
                    dp.append(i)

        return max_len


if __name__ == '__main__':
    nums = [1, 2, 1, 1, 3]
    k = 2
    # nums = [5, 4]
    # k = 0
    so = Solution()

    print(so.maximumLength(nums, k))
